package org.proteored.miapeapi.xml.ms.adapter;

import java.util.List;
import java.util.Set;

import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.ms.ActivationDissociation;
import org.proteored.miapeapi.interfaces.ms.Analyser;
import org.proteored.miapeapi.interfaces.ms.Esi;
import org.proteored.miapeapi.interfaces.ms.InstrumentConfiguration;
import org.proteored.miapeapi.interfaces.ms.Maldi;
import org.proteored.miapeapi.interfaces.ms.Other_IonSource;
import org.proteored.miapeapi.xml.ms.autogenerated.MSInstrumentConfiguration;
import org.proteored.miapeapi.xml.ms.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.ms.util.MsControlVocabularyXmlFactory;

public class InstrumentConfigurationAdapter implements Adapter<MSInstrumentConfiguration> {
	private final InstrumentConfiguration instrumentConfiguration;
	private final ObjectFactory factory;
	private final MsControlVocabularyXmlFactory cvFactory;

	public InstrumentConfigurationAdapter(InstrumentConfiguration instrumentConfiguration,
			ObjectFactory factory, MsControlVocabularyXmlFactory cvFactory) {
		this.cvFactory = cvFactory;
		this.factory = factory;
		this.instrumentConfiguration = instrumentConfiguration;
	}

	@Override
	public MSInstrumentConfiguration adapt() {
		MSInstrumentConfiguration xmlInstrumentConfiguration = factory
				.createMSInstrumentConfiguration();
		xmlInstrumentConfiguration.setName(instrumentConfiguration.getName());

		Set<ActivationDissociation> collisionCells = instrumentConfiguration
				.getActivationDissociations();
		if (collisionCells != null) {
			for (ActivationDissociation collisionCell : collisionCells) {
				xmlInstrumentConfiguration.getMSActivationDissociation().add(
						new CollisionCellAdapter(collisionCell, factory, cvFactory).adapt());
			}
		}
		List<Esi> esis = instrumentConfiguration.getEsis();
		if (esis != null) {
			for (Esi esi : esis) {
				xmlInstrumentConfiguration.getMSESI().add(
						new EsiAdapter(esi, factory, cvFactory).adapt());
			}

		}
		List<Analyser> analyzers = instrumentConfiguration.getAnalyzers();
		if (analyzers != null) {
			for (Analyser analyzer : analyzers) {
				xmlInstrumentConfiguration.getMSAnalyzer().add(
						new AnalyzerAdapter(analyzer, factory, cvFactory).adapt());
			}
		}
		// Set<IonOptic> ionOptics = instrumentConfiguration.getIonOptics();
		// if (ionOptics != null) {
		// for (IonOptic ionOptic : ionOptics) {
		// xmlInstrumentConfiguration.getMSIonOptic().add(
		// new IonOpticAdapter(ionOptic, factory, cvFactory).adapt());
		// }
		// }
		List<Maldi> maldis = instrumentConfiguration.getMaldis();
		if (maldis != null) {
			for (Maldi maldi : maldis) {
				xmlInstrumentConfiguration.getMSMALDI().add(
						new MaldiAdapter(maldi, factory, cvFactory).adapt());
			}
		}
		List<Other_IonSource> otherIonSources = instrumentConfiguration.getOther_IonSources();
		if (otherIonSources != null) {
			for (Other_IonSource other_IonSource : otherIonSources) {
				xmlInstrumentConfiguration.getMSOtherIonSource().add(
						new OtherIonSourceAdapter(other_IonSource, factory, cvFactory).adapt());
			}
		}
		return xmlInstrumentConfiguration;
	}

}
